package cn.com.oceansoft.osc.ms.schedule.service;


import ch.qos.logback.core.joran.spi.XMLUtil;
import cn.com.oceansoft.osc.ms.schedule.dswebService.WeChatServiceStub;
import cn.com.oceansoft.osc.ms.schedule.entity.*;
import cn.com.oceansoft.osc.ms.utils.HttpClientUtil;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import cn.com.oceansoft.osc.ms.persistence.ICallPoliceMapper;
import cn.com.oceansoft.osc.ms.utils.XMLUtil2;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * Created by wangg on 2016/10/23.
 */
@Component
public class CallPoliceService {

    @Resource
    ICallPoliceMapper callpoliceMapper;

    //推送微信报警的信息到指挥中心
    public boolean policeReport2DS(CallPolice callPolice, List<CallPoliceAttach> attaches,String guid) throws Exception {
        System.out.println("=====推送微信报警的信息到指挥中心=====");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date());
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<EpointDataBody>" +
                "<ReturnInfo>" +
                "<IsSuccess>True</IsSuccess>" +
                "<Msg>微信报警数据提交</Msg>" +
                "</ReturnInfo>" +
                "<ResultData>" +
                "<COMPLAINTLIST><GUID>" + callPolice.getGuid() + "</GUID>" +
                "<ADDRESS>" + callPolice.getAddress() + "</ADDRESS>" +
                "<CRCONTENT>" + callPolice.getCrContent() + "</CRCONTENT>" +
                "<ISPHONE>" + callPolice.getIsPhone() + "</ISPHONE>" +
                "<CREATEDATE>" + date + "</CREATEDATE>" +
                "<USERCODE>" + callPolice.getUserCode() + "</USERCODE>" +
                "<USERNAME>" + callPolice.getUserName() + "</USERNAME>" +
                "<USERPHONE>" + callPolice.getUserPhone() + "</USERPHONE>" +
                "<USERADDRESS>" + callPolice.getUserAddress() + "</USERADDRESS>" +
                "<UNITCONTACTOR>" + callPolice.getUnitcontactor() + "</UNITCONTACTOR>" +
                "<HANDPHONE>" + callPolice.getHandPhone() + "</HANDPHONE>" +
                "<TOORGID>" + callPolice.getToorGid() + "</TOORGID>" +
                "<TOORGNAME>" + callPolice.getToorgName() + "</TOORGNAME>" +
                "<COMNO>" + callPolice.getComno() + "</COMNO>" +
                "<OPENID>" + callPolice.getOpenid() + "</OPENID>" +
                "<LONG>" + callPolice.getLongitude() + "</LONG>" +
                "<LAT>" + callPolice.getLatitude() + "</LAT>" +
                "<REMARK1>" + callPolice.getTemp1() + "</REMARK1>" +
                "<REMARK2>" + callPolice.getTemp2() + "</REMARK2>" +
                "<Material>";
        if (CollectionUtils.isEmpty(attaches)) {
            xml += "<MaterialList>" +
                    "<fguid></fguid>" +
                    "<fname></fname>" +
                    "<attachtype></attachtype>" +
                    " <attachpath></attachpath>" +
                    "</MaterialList>";
        } else {
            for (CallPoliceAttach attach : attaches) {
                xml += "<MaterialList>" +
                        "<fguid>" + attach.getFguid() + "</fguid>" +
                        "<fname>" + attach.getFname() + "</fname>" +
                        "<attachtype>" + attach.getAttachType() + "</attachtype>" +
                        " <attachpath>" + attach.getAttachPath() + "</attachpath>" +
                        "</MaterialList>";
            }
        }
        xml += "</Material></COMPLAINTLIST>" +
                "</ResultData>" +
                "</EpointDataBody>";
        System.out.println(xml);
        WeChatServiceStub stub = new WeChatServiceStub();
        WeChatServiceStub.DS_AcceptWeChatAlarm chatAlarm = new WeChatServiceStub.DS_AcceptWeChatAlarm();
        chatAlarm.setStrXml(xml);
        String res = stub.DS_AcceptWeChatAlarm(chatAlarm).getDS_AcceptWeChatAlarmResult();
        String issucc1=String.valueOf( DocumentHelper.parseText(res).getRootElement().element("ReturnInfo").element("IsSuccess").getData());
        if (issucc1.equals("True")) {
            //添加日志
            CreateLog(xml, res, "1", "", "",guid,"aralm");
            return true;
        } else {
            //添加日志
            CreateLog(xml, res, "0", "", "",guid,"aralm");
            return false;
        }
    }

    //推送微信会话的信息到指挥中心
    public boolean policeReportChatDS(CallPoliceChatDS callPolice, String guid) throws Exception {
        System.out.println("=====推送微信报警的会话信息到指挥中心=====");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date());
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<paras>" +
                "<GUID>"+callPolice.getGUID()+"</GUID>" +
                "<PHONE>"+callPolice.getPHONE()+"</PHONE>" +
                "<OPENID>"+callPolice.getOPENID()+"</OPENID>" +
                "<CHANGETYPE>"+callPolice.getCHANGETYPE()+"</CHANGETYPE>" +
                "<CHANGECONTENT>"+callPolice.getCHANGECONTENT()+"</CHANGECONTENT>" +
                "<CHANGEDATE>"+callPolice.getCHANGEDATE()+"</CHANGEDATE>" +
                "<LONG>"+callPolice.getLONG()+"</LONG>" +
                "<LAT>"+callPolice.getLAT()+"</LAT>" +
                "<REMARK1>"+callPolice.getREMARK1()+"</REMARK1>" +
                "<REMARK2>"+callPolice.getREMARK2()+"</REMARK2>" +
                "</paras>";
        System.out.println(xml);
        WeChatServiceStub stub = new WeChatServiceStub();
        WeChatServiceStub.DS_AcceptChangeInfo chatAlarm = new WeChatServiceStub.DS_AcceptChangeInfo();
        chatAlarm.setStrXml(xml);
        String res = stub.DS_AcceptChangeInfo(chatAlarm).getDS_AcceptChangeInfoResult();
        System.out.println(res);
        String issucc1=String.valueOf( DocumentHelper.parseText(res).getRootElement().element("ReturnInfo").element("IsSuccess").getData());
        if (issucc1.equals("True")) {
            //添加日志
            CreateLog(xml, res, "1", "", "",guid,"chat");
            return true;
        } else {
            //添加日志
            CreateLog(xml, res, "0", "", "",guid,"chat");
            return false;
        }
    }

    //添加日志
    public boolean CreateLog(String xml, String res, String upstate, String msg, String openid,String temp1,String temp2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CallPoliceLog callPoliceLog = new CallPoliceLog();
        String guid = UUID.randomUUID().toString();
        callPoliceLog.setGUID(guid);
        callPoliceLog.setCREATETIME(df.format(new Date()).toString());
        callPoliceLog.setUPSTATE(upstate);
        callPoliceLog.setMSG(msg);
        callPoliceLog.setXML(xml);
        callPoliceLog.setRES(res);
        callPoliceLog.setOPENID(openid);
        callPoliceLog.setTEMP1(temp1);
        callPoliceLog.setTEMP2(temp2);
        callPoliceLog.setTEMP3("");
        int i = callpoliceMapper.insertLog(callPoliceLog);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }


    public boolean callPoliceChat2DS(CallPoliceChat callPoliceChat) throws Exception {
        String xml = "<paras><GUID>" + callPoliceChat.getGUID() + "</GUID>" +
                "<PHONE>" + callPoliceChat.getPHONE() + "</PHONE>" +
                "<OPENID>" + callPoliceChat.getOPENID() + "</OPENID>" +
                "<CHANGETYPE>" + callPoliceChat.getCHANGETYPE() + "</CHANGETYPE>" +
                "<CHANGECONTENT>" + callPoliceChat.getCHANGECONTENT() + "</CHANGECONTENT>" +
                "<CHANGEDATE>" + callPoliceChat.getCHANGEDATE() + "</CHANGEDATE>" +
                "<LONG>" + callPoliceChat.getLONGITUDE() + "</LONG>" +
                "<LAT>" + callPoliceChat.getLATITUDE() + "</LAT>" +
                "<REMARK1>" + callPoliceChat.getREMARK1() + "</REMARK1>" +
                "<REMARK2>" + callPoliceChat.getREMARK2() + "</REMARK2></paras>";
        WeChatServiceStub stub = new WeChatServiceStub();
        WeChatServiceStub.DS_AcceptChangeInfo dsAcceptChangeInfo = new WeChatServiceStub.DS_AcceptChangeInfo();
        dsAcceptChangeInfo.setStrXml(xml);
        String res = stub.DS_AcceptChangeInfo(dsAcceptChangeInfo).getDS_AcceptChangeInfoResult();
//        String result = XMLUtil.xml2String(res, "ReturnInfo");
//        JSONObject jsonObject = JSONObject.parseObject(result);
//        System.out.println("=====微信用户发送消息给DS=====");
        //log.info("微信报警DS返回值：" + jsonObject.toJSONString());
        //System.out.println("=====微信用户发送消息给DS=====");
        //return Boolean.parseBoolean(jsonObject.get("IsSuccess").toString().toLowerCase());
        return false;
    }
}


